package org.jivesoftware.openfire.plugin.chatlogs;

import org.jivesoftware.util.JiveGlobals;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLDecoder;
import java.net.URLEncoder;

public class HttpUtils {

    public static String BASE_URL;
    public static String PUSH_URL;
    public static String GROUP_LOG_URL;
    public static String PUSH_GROUP_URL;

    static {
        BASE_URL = JiveGlobals.getProperty("ADDRESS_BOOK_SYNC_LIST");
        PUSH_URL = JiveGlobals.getProperty("ADDRESS_BOOK_PUSH_MSG");
        PUSH_GROUP_URL = JiveGlobals.getProperty("ADDRESS_BOOK_PUSH_GROUP_MSG");
        GROUP_LOG_URL = JiveGlobals.getProperty("GROUP_CHAT_LOG");
    }

    public static String send(String urlStr,String callURL) {
        try {
            URL url = new URL(urlStr);
                HttpURLConnection connection = null;
                connection = (HttpURLConnection) url.openConnection();
                connection.setRequestMethod("POST");
                connection.setDoOutput(true);
                connection.setDoInput(true);
                connection.connect();

                DataOutputStream out = new DataOutputStream(connection
                        .getOutputStream());
                String param = URLEncoder.encode(callURL, "UTF-8");
                out.writeUTF(param);
                out.flush();
                out.close();
                int rc = connection.getResponseCode();
                // 响应成功
                if (rc == 200) {
                    String temp;
                    InputStream in = null;
                    in = connection.getInputStream();
                    BufferedReader data = new BufferedReader(new InputStreamReader(
                            in, "utf-8"));
                    StringBuffer result = new StringBuffer();
                    while ((temp = data.readLine()) != null) {
                        result.append(temp);
                        temp = null;
                    }
                    data.close();
                in.close();
                return result.toString();
            }
        } catch (IOException io) {
            io.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


}
